<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","nobkt");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_approval_cbrc.php";
$FileTemplate  = "ais_approval_cbrc_list.html";
$TableName     = "ais_memorial_z";
$SubHeaderTitle= "Approval Cash/Bank - Received";
$PageSize      = 20 ;
$HTMLFormName   = "MemorialTransForm";
$HTMLGridList  = "CashBankGrid";
//************ FORM ACTION **************
        if (GetParam("Action","")=="Delete") {
                DeleteRecord ("txtCheck",$PageSize);
        }elseif (GetParam("Action","")=="Approval") {
                ApprovalRecord ("txtCheck",$PageSize);
        }

//Initiate Database Table For Search;
$FCode = GetParam("transNoSearch","");
$RCode = GetParam("transNoRef","");
$Ftglbkt = GetParam("tglbkt","");


$PreserveGet = GetQueryString("All", array("PageNum"));
//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName` ";
$SQLCount = "SELECT count(*) FROM `$TableName` ";
$WHERE  = "ktrasl='".GetOfficeID()."' ";
$WHERE  .= " AND (nobkt like 'ZTK%' OR nobkt like 'ZTB%')";

$WHERE  .= " AND urut like '999' AND ".QueryLedger();

//Retrieve Parameter from search
if ($FCode) {
        $CustomWhere = " nobkt like '".$FCode."%'";
        if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
        else $WHERE = $CustomWhere;
}

if ($RCode) {
        $CustomWhere = " noref like '".$RCode."%'";
        if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
        else $WHERE = $CustomWhere;
}

if ($Ftglbkt) {
        list($day,$month,$year) = explode("/",GetParam("tglbkt",""));
        $Ftglbkt2   = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
        $CustomWhere = " tglbkt = '".$Ftglbkt2."'";
        if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
        else $WHERE = $CustomWhere;
//die($WHERE);
}

if (strlen($WHERE)) {
        $SQL = $SQL."WHERE ".$WHERE;
        //$SQL = $SQL. " ORDER BY noref "
        $SQLCount .= "WHERE ".$WHERE;
        //die($SQL);
}
else $SQL = $SQL;
$SQL  .= " ORDER BY noref ";
//Select Customer
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
/*
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;
*/
if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
//print $SQL;
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
        global $DBConnection,$FileName,$TableName;
        if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;

        }
        for ($x=$StartRecord;$x<=$EndRecord;$x++) {
                if (GetParam($sequence_name.$x,""))        {
                        $SQL = "SELECT noref FROM $TableName WHERE nobkt='".GetParam($sequence_name.$x,"")."'";
                        $RefNo = $DBConnection->dbc->get_var($SQL);
                        if (ereg("KW",$RefNo)) {
                            $SQL = "UPDATE ais_receipt SET aslref='' WHERE nokwit='$RefNo'";
                            $DBConnection->dbc->query($SQL);
                        }
                        $DBConnection->Delete("$TableName",$sequence_name.$x);
                }
        }
        header("Location: $FileName");
        exit;

}

function ApprovalRecord ($sequence_name,$PageSize) {
        global $DBConnection,$FileName,$TableName;
        if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;

        }
        for ($x=$StartRecord;$x<=$EndRecord;$x++) {
                if (GetParam($sequence_name.$x,""))        {
                        SetTransApproval(GetParam("Type",""),GetParam($sequence_name.$x,""));
                        //$DBConnection->Delete("$TableName",$sequence_name.$x);
            }
        }
        header("Location: $FileName");
        exit;

}

function SetTransApproval($appr_level,$nobkt) {
        global $DBConnection;
        if (!(ereg("TB",$nobkt) || ereg("TK",$nobkt) || ereg("BB",$nobkt) || ereg("BK",$nobkt))) return;
        if ($appr_level=="Approval1") {
                CheckAndUpdateAppr("Approval1",$nobkt);
                SetLogActivity($nobkt,"Approval1","");
        }elseif ($appr_level=="Approval2") {
                CheckAndUpdateAppr("Approval1",$nobkt);
                CheckAndUpdateAppr("Approval2",$nobkt);
                MoveRecord($nobkt);
        //CheckAndUpdateCache ($nobkt);
                SetLogActivity($nobkt,"Approval2","");

        }else {
                CheckAndUpdateAppr("Approval1",$nobkt);
                CheckAndUpdateAppr("Approval2",$nobkt);
                CheckAndUpdateAppr("Approval3",$nobkt);

        }
}

function GetTransDate($nobkt) {
  global $DBConnection;
  $SQL = "SELECT date_format(tglbkt,'%d/%m/%Y') FROM ais_memorial_z WHERE nobkt='$nobkt' ";
  return $DBConnection->dbc->get_var($SQL);
}


function MoveRecord($nobkt) {
                global $DBConnection;
                //Set Number on tblnumbering
                ereg("TB|TK|BB|BK",$nobkt,$Mod);
                if (($Mod[0]=="TB")||($Mod[0]=="BB")) {
                        $GetCode = $DBConnection->dbc->get_var("SELECT kdbank FROM ais_memorial_z WHERE nobkt='$nobkt'");
                }elseif (($Mod[0]=="TK")||($Mod[0]=="BK")) {
                        $GetCode = $DBConnection->dbc->get_var("SELECT kdcash FROM ais_memorial_z WHERE nobkt='$nobkt'");
                }

                $JumlahToBalance = $DBConnection->dbc->get_var("SELECT jumlah FROM ais_memorial_z WHERE nobkt='$nobkt' AND urut=999");
                $CurrentDate = GetTransDate($nobkt);
                if (strlen($GetCode))
                $ObjNumber  = new autonumber($CurrentDate,$GetCode);
                else
                $ObjNumber  = new autonumber($CurrentDate);
                $TransNo = $ObjNumber->GetNumber($Mod[0]);
                //$Ledger = GetPeriod($CurrentDate);
                $SytemDate   = date("Y-m-d");

                //Update Number on ais_memorial_z
                $SQL = "UPDATE ais_memorial_z SET nobkt='$TransNo' WHERE nobkt='$nobkt'";
                $DBConnection->dbc->query($SQL);
                //Update Number on ais_piutang_z
                /*
                $SQL = "UPDATE ais_piutang_z SET nobkt='$TransNo'  WHERE nobkt='$nobkt'";
                $DBConnection->dbc->query($SQL);
                */

                //Migrate data from ais_memorial_z to ais_memorial
                $SQL = "INSERT INTO ais_memorial (".GetColumnStruct("ais_memorial", "memorial_id").") SELECT ".GetColumnStruct("ais_memorial", "memorial_id")." FROM ais_memorial_z WHERE nobkt='$TransNo'";
                $DBConnection->dbc->query($SQL);
/*
                $SQL = "INSERT INTO ais_piutang SELECT * FROM ais_piutang_z WHERE nobkt='$TransNo'";
                $DBConnection->dbc->query($SQL);
*/

                SaveAging ($TransNo,1);
                //$DBConnection->dbc->debug();

                //Delete ais_memorial_z
                $SQL = "DELETE FROM ais_memorial_z WHERE nobkt='$TransNo'";
                $DBConnection->dbc->query($SQL);
/*
                $SQL = "DELETE FROM ais_piutang_z WHERE nobkt='$TransNo'";
                $DBConnection->dbc->query($SQL);
*/

                //Update Bank Or Cash
                if ($Mod[0]=="TB") {
                  $SQL = "UPDATE tblbank SET balance=balance+$JumlahToBalance WHERE kdbank='".$GetCode."'" ;
                } elseif ($Mod[0]=="TK") {
                  $SQL = "UPDATE tblcash SET balance=balance+$JumlahToBalance WHERE kdcash='".$GetCode."'" ;
                }
                $DBConnection->dbc->query($SQL);

                //Update History Transaction
                $SQL = "SELECT * FROM ais_memorial WHERE nobkt='$TransNo' AND urut=999";
                $MyResult = $DBConnection->dbc->get_row($SQL);

                if ($MyResult) {
                    if (ereg("KW",$MyResult->noref)) {
                        $SQL = "UPDATE ais_receipt SET aslref='".$TransNo."' WHERE nokwit='".$MyResult->noref."'";
                        $DBConnection->dbc->query($SQL);
                    }
                /*
                  $SQL = "UPDATE ais_memorial_history SET memo_jour='".$MyResult->nobkt."', jml_memo='".$MyResult->jumlah."' WHERE memoesti='".$MyResult->aslref."' AND ktrasl='".GetOfficeID()."'";
                  $DBConnection->dbc->query($SQL);
                  $DBConnection->dbc->debug();
                  */
                }

}

function FlushCustomer ($TableName, $tmp) {
        global $DBConnection;

        if ($tmp) $TEMPORARY = "TEMPORARY";
        $DBC = new mydb(DB_USER, DB_PASSWORD, DB_NAME2, DB_HOST);

        $SQL = "SELECT kodecustomer,nama,cr_term,kdtype FROM customer ";
        if (GetParam("kdtype","")) $SQL .= " WHERE kdtype='".GetParam("kdtype","")."'";
        $MyResult = $DBC->dbc->get_results($SQL,ARRAY_N);
        if (!$MyResult) return false;
         $session_file = substr(md5(time()), 0, 8);
           $session_file = "/tmp/".$session_file.".tmp";
        BufferToFile ($MyResult,$session_file);

        $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        $SQL = "DROP TABLE IF EXISTS `$TableName`;";
        $DBConnection->dbc->query($SQL);

        $SQL= "CREATE $TEMPORARY TABLE `$TableName` (
                          `kodecustomer` varchar(20) NOT NULL default '',
                          `nama` varchar(50) default NULL,
                          `cr_term` int(4) default NULL,
                          `kdtype` varchar(20) default NULL
                          )";
        $DBConnection->dbc->query($SQL);

           $SQL = "LOAD DATA INFILE '$session_file' INTO TABLE $TableName FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
        $DBConnection->dbc->query($SQL);
        unlink($session_file);
        return true;
}

function CheckAndUpdateAppr($appr_level,$nobkt){
        global $DBConnection,$FileName,$TableName;
        if ($appr_level=="Approval1") {
                $SQL = "SELECT app1 FROM ais_memorial_z WHERE nobkt='$nobkt'";
                $ApprStat = $DBConnection->dbc->get_var($SQL);
                if ($ApprStat<>'1')
                        $SQL = "UPDATE ais_memorial_z SET app1='1', nmapp1='".GetStaffName()."' WHERE nobkt = '$nobkt'";
                else return false;

        } elseif ($appr_level=="Approval2") {
                $SQL = "SELECT app2 FROM ais_memorial_z WHERE nobkt='$nobkt'";
                $ApprStat = $DBConnection->dbc->get_var($SQL);
                if ($ApprStat<>'1')
                        $SQL = "UPDATE ais_memorial_z SET app2='1', nmapp2='".GetStaffName()."' WHERE nobkt = '$nobkt'";
                else return false;
        } else {
                $SQL = "SELECT app3 FROM ais_memorial_z WHERE nobkt='$nobkt'";
                $ApprStat = $DBConnection->dbc->get_var($SQL);
                if ($ApprStat<>'1')
                        $SQL = "UPDATE ais_memorial_z SET app3='1', nmapp3='".GetStaffName()."' WHERE nobkt = '$nobkt'";
                else return false;
        }
        $DBConnection->dbc->query($SQL);
}

function GetLastDateReport($fromSkada,$toSkada,$ktrasl,$curr,$tglbkt) {
        global $DBConnection;
        list($day,$month,$year) = explode("/",$tglbkt);
        $current_date = "$year-$month-$day";

        $SQL = "DELETE FROM ais_piutang_sisa WHERE aging_date>='$current_date' ";
        if ($curr) $SQL .= " AND curr='".$curr."' ";
        if ($fromSkada) $SQL .= " AND fromskada = '".$fromSkada."' ";
        if ($toSkada) $SQL .= " AND toskada = '".$toSkada."' ";
        $DBConnection->dbc->query($SQL);
        //$DBConnection->dbc->debug();

        $SQL = "SELECT max(aging_date) FROM ais_piutang_sisa WHERE aging_date<='$current_date' ";
        if ($curr) $SQL .= " AND curr='".$curr."' ";
        if ($fromSkada) $SQL .= " AND fromskada = '".$fromSkada."' ";
        if ($toSkada) $SQL .= " AND toskada = '".$toSkada."' ";

        return $DBConnection->dbc->get_var($SQL);
}

function CheckAndUpdateCache ($nobkt) {
//check debit note
              $SQL = "SELECT skada,curr,ktrasl,tglbkt FROM ais_piutang WHERE nobkt='$nobkt' AND normut=999 ";
              $MyResult = $DBConnection->dbc->get_results($SQL, ARRAY_A);
            foreach($MyResult as $row)
            {
              GenerateAgingCache($row["skada"],$row["skada"],$row["ktrasl"],$row["curr"],$row["tglbkt"]);
            }
}

function GenerateAgingCache($fromSkada,$toSkada,$ktrasl,$curr,$tglbkt) {
                         global $DBConnection;
                        //$TEMP = "TEMPORARY";

                         if (!FlushCustomer ("ais_customer", 1)) return false;
                        //$current_date = date("Y-m-d",mktime (0,0,0,GetParam("m",""),1,GetParam("y","")));

                        list($day,$month,$year) = explode("/",$tglbkt);
                        $current_date = "$year-$month-$day";
                        $LastDate  = GetLastDateReport($fromSkada,$toSkada,$ktrasl,$curr,$tglbkt);

                        $SQL = "DROP TABLE IF EXISTS `ais_piutang_terhutang`";
                        $DBConnection->dbc->query($SQL);

                        $SQL = "
                                        CREATE $TEMP TABLE `ais_piutang_terhutang` (
                                          `ktrasl` varchar(5) default NULL,
                                          `tglbkt` date,
                                          `nobkt` varchar(16) default NULL,
                                          `nobkta` varchar(16) default NULL,
                                          `debtur` varchar(16) default NULL,
                                          `debnam` varchar(32) default NULL,
                                          `uraian` text default NULL,
                                          `curr` varchar(4) default NULL,
                                          `jumlah` varchar(32) default NULL,
                                          `cr_term` tinyint(4)default NULL,
                                          `top_curr` date,
                                          `top_30` date,
                                          `top_60` date,
                                          `aging` varchar(16)
                                        );
                                   ";
                        $DBConnection->dbc->query($SQL);

                        $SQL = "
                                        INSERT INTO ais_piutang_terhutang
                                        SELECT KTRASL, TGLBKT, NOBKT,NOBKTA, ais_customer.kodecustomer, ais_customer.nama, URAIAN, CURR, JUMLAH, IFNULL(CR_TERM,0) AS TOP,

                                        ADDDATE( tglbkt,INTERVAL ais_customer.cr_term DAY ) as top_curr,
                                        ADDDATE( tglbkt,INTERVAL 30+ais_customer.cr_term DAY ) as top_30,
                                        ADDDATE( tglbkt,INTERVAL 60+ais_customer.cr_term DAY ) as top_60,

                                        IF('$current_date'<=ADDDATE( tglbkt,INTERVAL ais_customer.cr_term DAY ),'0',
                                        IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 30+ais_customer.cr_term DAY ),'30',
                                        IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 60+ais_customer.cr_term DAY ),'60','60g')))

                                        FROM ais_piutang
                                        LEFT JOIN ais_customer ON ais_piutang.DEBTUR=ais_customer.kodecustomer
                                        WHERE srt='1' AND ais_piutang.tglbkt<='$current_date'
                                   ";

                        if (strlen($LastDate))
                        $SQL .= "AND ais_piutang.tglbkt>'$LastDate' ";

                        if ($fromSkada)
                        $SQL .= "AND ais_piutang.skada >= '".$fromSkada."' ";

                        if ($curr)
                        $SQL .= "AND ais_piutang.curr = '".$curr."' ";

                        if ($toSkada)
                        $SQL .= "AND ais_piutang.skada <= '".$toSkada."' ";

                        //print $SQL;die();
                        if ($ktrasl)  $SQL .= "AND ais_piutang.ktrasl = '".$ktrasl."' ";

                        $SQL .= "order by nobkt ";
                        $DBConnection->dbc->query($SQL);

                        $SQL = "DROP TABLE IF EXISTS `ais_piutang_terbayar`";
                        $DBConnection->dbc->query($SQL);

                        $SQL = "
                                        CREATE $TEMP TABLE `ais_piutang_terbayar` (
                                          `nobkt` varchar(16) default NULL,
                                          `nobkta` varchar(16) default NULL,
                                          `bayar` decimal(15,3) default NULL,
                                          `debtur` varchar(64) default NULL,
                                          `debnam` varchar(128) default NULL
                                        ) ;
                                   ";
                        $DBConnection->dbc->query($SQL);

                        $SQL = "
                                        INSERT INTO ais_piutang_terbayar
                                        SELECT NOBKT, NOBKTA, SUM(JUMLAH),debtur,debnam
                                        FROM ais_piutang
                                        LEFT JOIN ais_customer ON ais_piutang.DEBTUR=ais_customer.kodecustomer
                                        WHERE srt='2' AND ais_piutang.tglbkt<='$current_date' ";

                        if (strlen($LastDate))
                        $SQL .= "AND ais_piutang.tglbkt>'$LastDate' ";

                        //print $SQL;die();
                        if (GetOfficeID())
                        $SQL .= "AND ais_piutang.ktrasl = '".GetOfficeID()."' ";

                        if ($curr)
                        $SQL .= "AND ais_piutang.curr = '".$curr."' ";

            $SQL .= "group by nobkta order by nobkta";
                        //print $SQL;die();
                        $DBConnection->dbc->query($SQL);
                        //$DBConnection->dbc->debug();

//Auto Update Using Cache Begin Here
                         if (strlen($LastDate)) {

                                $SQL = "DROP TABLE IF EXISTS `ais_piutang_sisa_period`";
                                $DBConnection->dbc->query($SQL);

                                $SQL = "
                                                CREATE $TEMP TABLE `ais_piutang_sisa_period` (
                                                  `ktrasl` varchar(5) default NULL,
                                                  `tglbkt` date,
                                                  `aging_date` date,
                                                  `nobkt` varchar(16) default NULL,
                                                  `debtur` varchar(16) default NULL,
                                                  `debnam` varchar(32) default NULL,
                                                  `uraian` text default NULL,
                                                  `cr_term` tinyint(4)default NULL,
                                                  `jumlah` decimal(15,2) default NULL,
                                                  `aging` varchar(16),
                                                  `fromskada` varchar(16),
                                                  `toskada` varchar(16),
                                                  `curr`varchar(5)
                                                )
                                           ";
                                $DBConnection->dbc->query($SQL);
                     $SQL = "
                              INSERT INTO ais_piutang_sisa_period
                              SELECT *
                              FROM ais_piutang_sisa
                              WHERE aging_date='$LastDate' AND fromskada='".$fromSkada."' AND toskada='".$toSkada."'
                                                ";
                              if ($curr)  $SQL .= " AND curr='".$curr."' ";

                                 $DBConnection->dbc->query($SQL);

                       $SQL = "
                                       INSERT INTO ais_piutang_sisa_period
                                  SELECT ktrasl,tglbkt,'$current_date',nobkt,debtur,debnam,uraian,cr_term,jumlah,aging,'".$fromSkada."','".$toSkada."','".$curr."'
                                  FROM ais_piutang_terhutang
                            ";
                                 $DBConnection->dbc->query($SQL);


                             $SQL = "
                                      INSERT INTO ais_piutang_sisa
                                      SELECT
                                      ais_piutang_sisa_period.ktrasl,
                                      ais_piutang_sisa_period.tglbkt,
                                      '$current_date',
                                      ais_piutang_sisa_period.nobkt,
                                      ais_piutang_sisa_period.debtur,
                                      ais_piutang_sisa_period.debnam,
                                      ais_piutang_sisa_period.uraian,
                                      ifnull(ais_piutang_sisa_period.cr_term, 0) AS top,
                                      ifnull(ais_piutang_sisa_period.jumlah,0)-ifnull(ais_piutang_terbayar.bayar,0),

                                          IF('$current_date'<=ADDDATE( tglbkt,INTERVAL ais_piutang_sisa_period.cr_term DAY ),'0',
                                          IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 30+ais_piutang_sisa_period.cr_term DAY ),'30',
                                          IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 60+ais_piutang_sisa_period.cr_term DAY ),'60','60g'))),
                                          '".$fromSkada."','".$toSkada."','".$curr."'

                                      FROM
                                      ais_piutang_sisa_period
                                      LEFT JOIN ais_piutang_terbayar ON (ais_piutang_sisa_period.nobkt = ais_piutang_terbayar.nobkta)
                                      WHERE (ifnull(ais_piutang_terbayar.nobkt, '') = '') OR ifnull(ais_piutang_sisa_period.jumlah-ais_piutang_terbayar.bayar,0) <> 0
                                      ORDER BY debtur,nobkt
                                    ";
                                         $DBConnection->dbc->query($SQL);

                         } else {
                             $SQL = "
                                      INSERT INTO ais_piutang_sisa
                                      SELECT
                                      ais_piutang_terhutang.ktrasl,
                                      ais_piutang_terhutang.tglbkt,
                                      '$current_date',
                                      ais_piutang_terhutang.nobkt,
                                      ais_piutang_terhutang.debtur,
                                      ais_piutang_terhutang.debnam,
                                      ais_piutang_terhutang.uraian,
                                      ifnull(ais_piutang_terhutang.cr_term, 0) AS top,
                                      ifnull(ais_piutang_terhutang.jumlah,0)-ifnull(ais_piutang_terbayar.bayar,0),

                                    IF('$current_date'<=ADDDATE( tglbkt,INTERVAL ais_piutang_terhutang.cr_term DAY ),'0',
                                    IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 30+ais_piutang_terhutang.cr_term DAY ),'30',
                                    IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 60+ais_piutang_terhutang.cr_term DAY ),'60','60g'))),
                                          '".$fromSkada."','".$toSkada."','".$curr."'

                                      FROM
                                      ais_piutang_terhutang
                                      LEFT JOIN ais_piutang_terbayar ON (ais_piutang_terhutang.nobkt = ais_piutang_terbayar.nobkta)
                                      WHERE (ifnull(ais_piutang_terbayar.nobkt, '') = '') OR ifnull(ais_piutang_terhutang.jumlah-ais_piutang_terbayar.bayar,0) <> 0
                                      ORDER BY debtur,nobkt
                                    ";
                                        $DBConnection->dbc->query($SQL);

                        }
//Auto Update Using Cache End Here

//                        $SQL = "SELECT * FROM ais_aging WHERE aging_date='$current_date'";

}

?>